#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <vector>
#include <queue>

#define INF 2000000000

using namespace std;

map<string,int> m;
vector< int > v[2001];//lista adjacencia
char pc[2001][2001];//primeira letra
int  pi[2001][2001];//comprimento letra
string a, b, c;
int n=1, i, j, k, x, y;

bool read(){
	scanf("%d", &n);
	if(!n)return 0;
	cin>>a>>b;
	m[a]=1;
	m[b]=2;
	for(k=2, i=0;i<n;++i){
		cin>>a>>b>>c;
		if(m[a]==0) m[a] = ++k;
		if(m[b]==0)	m[b] = ++k;
		
		x=m[a];
		y=m[b];
		
		printf("%d %d %s\n", x, y, c.c_str());
		v[x].push_back(y);
		v[y].push_back(x);
		pc[y][x]=pc[x][y]=c[0];
		pi[x][y]=pi[y][x]=c.size();
	}
	
	return 1;
}
char pre[2001];
int dist[2001], z;
bool mark[2001];
priority_queue< pair<int,int> > q;
pair<int,int> temp;
bool process(){
	for(i=1;i<=k;++i){
		printf("%d -> ", i);
		for(j=0;j<v[i].size();++j)printf("-  %d,%c%d   -", v[i][j], pc[i][v[i][j]], pi[i][v[i][j]]);
		printf("\n\n");
	}
	for(i=1;i<=k;++i){
		dist[i] = INF;
		mark[i] = 1;
	}
	dist[1]=0;
	pre[1]='0';
	q.push( pair<int,int> (0,1) );
	
	while(!q.empty()){
		temp=q.top();q.pop();
		i=temp.second;
		
		if(mark[i]){
			mark[i]=0;
			for(z=0;z<v[i].size();++z){
				j=v[i][z];//adj to i
				if(mark[j]&&dist[i]+pi[i][j]<dist[j]&&pc[i][j]!=pre[i]){
					dist[j] = dist[i]+pi[i][j];
					pre[j]=pc[i][j];
					q.push( pair<int,int> (-dist[j],j) );
				}
			}
		}
	}
	return dist[2]!=INF;
}

int main(){
	freopen("in.txt", "r", stdin);
	while(read()){
		if(process())printf("%d\n", dist[2]);
		else printf("impossivel\n");
	}
	return 0;
}
